//
//  TXPHomePageViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPHomePageViewController.h"
#import "TXPSelectionPageViewController.h"
#import "TXPSelectionPageViewController.h"
#import "TXPDynamicPageViewController.h"
#import "TXPFragmentViewController.h"

static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;

@interface TXPHomePageViewController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, retain) UIView *indicateView;
// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;
@property (nonatomic, strong) NSMutableArray *buttons;
@end

@implementation TXPHomePageViewController

- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor = RGBA(35, 35, 35, 1);
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 40)];
    self.label.text = @"首页";
    self.label.font = [UIFont systemFontOfSize:18];
    self.label.textColor = [UIColor whiteColor];
    [self.navigationController.view addSubview:self.label];
    
    [self setNavigation];
    [self setupTitleScrollView];
    [self setupContentScrollView];//创建大scrollview
    
    [self addChildViewController];//添加子控制器
    [self setupTitle];//根据子视图个数创建小scrollview的按钮
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * WIDTH, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    self.contentScrollView.bounces = NO;
    
  //  self.contentScrollView.userInteractionEnabled = NO;
    [self setNavigation];


}


#pragma make - 设置导航栏左边抽屉按钮
- (void)setNavigation
{
    UIImage *image = [UIImage imageNamed:@"iconfont-wechaticon24.png"];
    // Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
    [self.navigationItem.leftBarButtonItem setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
}

#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    
    CGRect rect = CGRectMake(0, navBarH, WIDTH, titleH);
    // 判断是否存在导航控制器来判断y值
  //  CGFloat y = self.navigationController ? navBarH : 0;
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleScrollView];
    self.titleScrollView = titleScrollView;
    self.indicateView = [[UIView alloc] initWithFrame:CGRectMake(0, 41, WIDTH / 3, 3)];
    
    self.indicateView.backgroundColor = RGBA(140, 158, 181, 1);
    [self.titleScrollView addSubview:self.indicateView];
}


#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, WIDTH, HEIGHT - navBarH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
   
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
    TXPSelectionPageViewController *vc = [[TXPSelectionPageViewController alloc] init];
    vc.title = @"精选";
    [self addChildViewController:vc];
    
    TXPDynamicPageViewController *vc1 = [[TXPDynamicPageViewController alloc] init];
    vc1.title = @"动态";
    [self addChildViewController:vc1];
    
    TXPFragmentViewController *vc2 = [[TXPFragmentViewController alloc] init];
    vc2.title = @"碎片";
    [self addChildViewController:vc2];
    
}

#pragma mark - 设置标题
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = WIDTH / 3;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:RGBA(54, 54, 54, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(UIButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * WIDTH;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}
// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    
    [self.selTitleButton setTitleColor:RGBA(54, 54, 54, 1) forState:UIControlStateNormal];
    [btn setTitleColor:RGBA(78, 101, 147, 1) forState:UIControlStateNormal];
    
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}

- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * WIDTH;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, WIDTH, HEIGHT - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}
- (void)setupTitleCenter:(UIButton *)btn{
    CGFloat offset = btn.center.x - WIDTH * 0.5;
    if (offset < 0) {
        offset = 0;
    }
    CGFloat maxOffset = self.titleScrollView.contentSize.width - WIDTH;
    if (offset > maxOffset) {
        offset = maxOffset;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / WIDTH;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat x = self.contentScrollView.contentOffset.x / self.contentScrollView.frame.size.width;
    x *= WIDTH / 3;
    CGRect frame = self.indicateView.frame;
    frame.origin.x = x;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.indicateView.frame = frame;
    [UIView commitAnimations];
   
    
    
    

}


- (void)left:(UIBarButtonItem *)bnt{
    if ([_delegate respondsToSelector:@selector(checkleftBarButtonItemRootViewController:)]) {
        [_delegate checkleftBarButtonItemRootViewController:self];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
